package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.StateForm;
import com.vcaresolution.logistic.pojo.State;
import com.vcaresolution.logistic.to.StateTO;

@Service
public class StateServiceImpl extends BaseService implements StateService{

	private State populateStatePojo(StateForm sectorForm){
		State sector = new State();
		BeanUtils.copyProperties(sectorForm, sector);
		
		return sector;
	}
	
	@Override
	@Transactional
	public void add(StateForm sectorForm) {
		State sector = populateStatePojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().add(sector);
	}

	@Override
	@Transactional
	public void update(StateForm sectorForm) {
		State sector = populateStatePojo(sectorForm);
		
		getDaoFactory().getUtilityDAO().update(sector);
	}

	@Override
	@Transactional
	public void delete(StateForm sectorForm) {
		getDaoFactory().getUtilityDAO().delete(populateStatePojo(sectorForm));
	}

	@Override
	@Transactional
	public StateTO findById(Serializable sectorID) {
		State sector = (State) getDaoFactory().getUtilityDAO().findById(State.class,sectorID);
		
		StateTO sectorTO = new StateTO();
		BeanUtils.copyProperties(sector, sectorTO);
		
		return sectorTO; 
	}

	@Override
	@Transactional
	public List<StateTO> findByAll() {
		List<State> sectorList = getDaoFactory().getUtilityDAO().findByAll(State.class);
		
		List<StateTO> sectorFormList = new ArrayList<StateTO>();
		
		if( sectorList != null && !sectorList.isEmpty() ){
			StateTO sectorForm = null;
			for(State sector: sectorList){
				sectorForm = new StateTO();
				
				BeanUtils.copyProperties(sector, sectorForm);
				sectorFormList.add(sectorForm);
			}
		}
		
		return sectorFormList;
	}

}
